/*
 * Copyright 2008 Lucifer Lu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using SilverChart.Configuration;
using SilverChart.Core.Log;

namespace SilverChart.Components {
    /// <summary>
    /// Description of Lengends.
    /// </summary>
    public class LegendsComponent : CompositeChartWidget, RelativeClientConsumer {
        private static Logger logger = Logger.GetLogger(typeof(LegendsComponent).FullName);

        private StackPanel legendContainer;
        private Legend LegendConfig;
        private Border legendsBorder;

        public LegendsComponent(Legend legendConfig)
            : base() {
            LegendConfig = legendConfig;
            //Background = BrushHelper.GetSolidColorBrush("Yellow");
            legendsBorder = LegendConfig.Border;
            logger.Debug("Get legendsBorder:" + legendsBorder.ToString());

            legendContainer = (StackPanel)legendsBorder.Child;
            logger.Debug("Get legendContainer:" + legendContainer.ToString());

            if (LegendConfig.Direction == LegendDirection.Horizontal) {
                legendContainer.Orientation = Orientation.Horizontal;
            }
            else {
                legendContainer.Orientation = Orientation.Vertical;
            }
            legendsBorder.HorizontalAlignment = LegendConfig.HorizontalAlignment;
            legendsBorder.VerticalAlignment = LegendConfig.VerticalAlignment;
        }

        public override void InitializeWidgets(){
            base.InitializeWidgets();

            IList<ChartBase> allCharts = WidgetsManager.GetCompatibleComponents<ChartBase>();
            foreach (ChartBase chart in allCharts) {
                IList<LegendProperties> chartLegends = chart.Legends;
                if (chartLegends == null) {
                    continue;
                }

                foreach (LegendProperties property in chartLegends) {
                    LegendComponent legend = new LegendComponent(property);
                    legendContainer.Children.Add(legend);
                }
            }

            Children.Add(legendsBorder);
        }

        protected override Size MeasureOverride(Size availableSize) {
            legendsBorder.Measure(availableSize);
            return new Size(legendsBorder.DesiredSize.Width, legendsBorder.DesiredSize.Height);
        }

        protected override Size ArrangeOverride(Size finalSize) {
            if (legendContainer.Children.Count > 0) {
                Double left = 0;
                Double top = 0;
                switch (LegendConfig.HorizontalAlignment) {
                    case HorizontalAlignment.Center:
                        left = (finalSize.Width - legendsBorder.DesiredSize.Width) / 2;
                        break;
                    case HorizontalAlignment.Right:
                        left = finalSize.Width - legendsBorder.DesiredSize.Width;
                        break;
                    case HorizontalAlignment.Left:
                    default:
                        left = 0;
                        break;
                }

                switch (LegendConfig.VerticalAlignment) {
                    case VerticalAlignment.Center:
                        top = (finalSize.Height - legendsBorder.DesiredSize.Height) / 2;
                        break;
                    case VerticalAlignment.Bottom:
                        top = finalSize.Height - legendsBorder.DesiredSize.Height;
                        break;
                    case VerticalAlignment.Top:
                    default:
                        top = 0;
                        break;
                }

                if (Dock != DockType.Left && Dock != DockType.Right) {
                    left += LegendConfig.HorizontalOffset;
                }

                if (Dock != DockType.Top && Dock != DockType.Bottom) {
                    top += LegendConfig.VerticalOffset;
                }

                legendsBorder.Arrange(new Rect(left, top, legendsBorder.DesiredSize.Width,
                    legendsBorder.DesiredSize.Height));
            }
            return finalSize;
        }

        public override string WidgetName {
            get {
                return "Legend Component";
            }
        }

        public override DockType Dock {
            get {
                if (!LegendConfig.OutOfChart) {
                    return DockType.Client;
                }
                else if (LegendConfig.Direction == LegendDirection.Horizontal) {
                    if (LegendConfig.VerticalAlignment == VerticalAlignment.Bottom) {
                        return DockType.Bottom;
                    }
                    return DockType.Top;
                }
                else {
                    if (LegendConfig.HorizontalAlignment == HorizontalAlignment.Left) {
                        return DockType.Left;
                    }
                    return DockType.Right;
                }
            }
        }

        #region RelativeClientConsumer Members

        public RelativeClientProvider GetClientProvider() {
            IList<CartesianCoordinateComposite> coordinates =
                WidgetsManager.GetCompatibleComponents<CartesianCoordinateComposite>();
            if (coordinates != null && coordinates.Count > 0) {
                return coordinates[0];
            }

            return null;
        }

        #endregion
    }
}
